package com.svm.operators;

import java.awt.image.BufferedImage;

import javax.media.jai.Interpolation;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;
import javax.media.jai.operator.ScaleDescriptor;

public class ImageResize {

	public static BufferedImage resize(String path, Integer width,
			Integer height) {

		RenderedOp renderedOp = null;

		try {

			PlanarImage image = JAI.create("fileload", path);

			float xScale = (float) width / image.getWidth();
			float yScale = 0f;

			if (height == 0) {

				yScale = xScale;

			} else {

				yScale = (float) height / (float) image.getHeight();
			}

			renderedOp = ScaleDescriptor.create(image, new Float(xScale),
					new Float(yScale), new Float(0.0f), new Float(0.0f),
					Interpolation.getInstance(Interpolation.INTERP_BICUBIC),
					null);

		} catch (Exception e) {
			System.out.println(e);
		}

		return renderedOp.getAsBufferedImage();
	}

}
